﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Meraz.Core.Managers;
using Meraz.Core.Model;

using Ninject;
using Meraz.Web.Filters;
using Meraz.Web.Models;
using Meraz.Web.Models.Twitter;
using Meraz.Core.Config;

namespace Meraz.Web.Controllers
{
    public class TwitterUserController : Controller
    {
        [Inject]
        public TwitterManager TwitterManager { get; set; }
        public ActionResult Index()
        {
            TwitterUsersModel model = new TwitterUsersModel();
            model.Users = TwitterManager.GetTwitterUsers();
            return View(model);
        }

        [RolesAuthorize(Roles = UserRoles.Editor)]
        [HttpPost]
        public ActionResult Index(string screen_name)
        {
            TwitterUser user = null;
            TwitterUsersModel model = new TwitterUsersModel();
            try
            {
                user = TwitterManager.AddUser(screen_name);
            }
            catch (Exception problem)
            {
                model.Message = "Error: " + problem.Message;
            }
            model.Users = TwitterManager.GetTwitterUsers();
            return View(model);
        }

        [RolesAuthorize(Roles = UserRoles.Editor)]
        [HttpPost]
        public ActionResult Add(string id)
        {
            //id is username
            TwitterUser user = null;
            try
            {
                user = TwitterManager.AddUser(id);
            }
            catch (Exception problem)
            {
                var response = this.HttpContext.Response;
                response.StatusCode = 600; //instead of 400 Bad Request
                response.AddHeader("MerazHeader", problem.Message);
                response.Write(problem.Message);
                response.End();
                return null;
            }
            RowModel model = new RowModel();
            if (user != null)
            {
                model.Message = "Success";
                model.Values.Add(user.ID.ToString());
                model.Values.Add(user.Name);
                model.Values.Add(user.IsBlog.ToString().ToLower());
                model.Values.Add(user.IsNewspaper.ToString().ToLower());
                model.Values.Add(user.IsTopJournalist.ToString().ToLower());
                model.Values.Add(user.IsTopPolitical.ToString().ToLower());
            }
            else
            {
                model.Message = "Fail";
            }
            return Json(model);
        }

        [RolesAuthorize(Roles = UserRoles.Editor)]
        [ObjectFilter(Param = "user", RootType = typeof(TwitterUser))]
        [HttpPost]
        public ActionResult Save(TwitterUser user)
        {
            if (!HttpContext.User.Identity.IsAuthenticated ||
            !System.Web.Security.Roles.IsUserInRole(UserRoles.Editor.ToString()))
                return null;
            RowModel model = new RowModel();
            if (TwitterManager.Save(user) != null)
            {
                model.Message = "Success";
                model.Values.Add(user.ID.ToString());
                model.Values.Add(user.Name);
                model.Values.Add(user.IsBlog.ToString().ToLower());
                model.Values.Add(user.IsNewspaper.ToString().ToLower());
                model.Values.Add(user.IsTopJournalist.ToString().ToLower());
                model.Values.Add(user.IsTopPolitical.ToString().ToLower());
            }
            else
            {
                model.Message = "Fail";
            }
            return Json(model);
        }

        [RolesAuthorize(Roles = UserRoles.Editor)]
        [HttpPost]
        public ActionResult Remove(string id)
        {
            //id is username
            if (string.IsNullOrWhiteSpace(id))
                throw new ApplicationException("username required!");
            bool result = TwitterManager.RemoveUser(id);
            RowModel model = new RowModel();
            model.Message = (result) ? "Success" : "Fail";
            return Json(model);
        }

        [HttpGet]
        public ActionResult Usernames()
        {
            RowModel model = new RowModel();
            model.Values = TwitterManager.GetTwitterUsers().Select(sel1 => sel1.Name).ToList();
            return Json(model, JsonRequestBehavior.AllowGet);
        }

    }
}
